﻿using System;
using System.Data;
using System.Web;
using System.Web.Security;

public partial class ModifyUserData : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		lblUserName.Text = System.Threading.Thread.CurrentPrincipal.Identity.Name;
		UpdateLanguageImages();

		DataSet dsUserGroups = GetDataSetUserGroups();
		if (dsUserGroups != null)
		{
			lblEmail.Text = dsUserGroups.Tables[0].Rows[0]["Email"].ToString();
		}
		phChangePassword.Visible = (HttpContext.Current.User.Identity.AuthenticationType != "Negotiate");
	}

	private void UpdateLanguageImages()
	{
		string language = Tools.GetLanguage(this.Request);
		System.Web.UI.WebControls.Image target = null;
		if (language.StartsWith("ro", StringComparison.InvariantCultureIgnoreCase))
			target = imgLang_ro;
		if (language.StartsWith("en", StringComparison.InvariantCultureIgnoreCase))
			target = imgLang_en;
		if (language.StartsWith("de", StringComparison.InvariantCultureIgnoreCase))
			target = imgLang_de;
		if (target != null)
		{
			target.BorderColor = System.Drawing.Color.Red;
			target.BorderStyle = System.Web.UI.WebControls.BorderStyle.Dotted;
			target.BorderWidth = 2;
		}
	}

	private DataSet GetDataSetUserGroups()
	{
		DataSet dsUserGroups = (DataSet)HttpContext.Current.Session["dsUserGroups"];
		if (dsUserGroups == null)
		{
            dsUserGroups = Hipocrate.DB.DBConnection.GetData("Groups_Select", new string[] { "@UserID" }, new object[] { GetUserID() }, false);
			HttpContext.Current.Session["dsUserGroups"] = dsUserGroups;
		}
		return dsUserGroups;
	}

	public string GetUserID()
	{
		if (Session["UserID"] == null)
		{
			Session["UserID"] = Tools.GetUserID(GetUserName(), "");
		}
		if (Session["UserID"] != null)
		{
			return Session["UserID"].ToString();
		}
		else
		{
			FormsAuthentication.SignOut();
			Response.Redirect(this.Page.Request.Url.ToString());
			return null;
		}
	}

	public string GetUserName()
	{
		return System.Threading.Thread.CurrentPrincipal.Identity.Name;
	}
}
